package cn.szh.dao;

import cn.szh.domain.Role;
import cn.szh.domain.UserInfo;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserDao {
    @Select("select * from users where username=#{username}")
    @Results({ @Result(id=true,column = "id",property = "id"),
               @Result(column = "username",property = "username"),
               @Result(column = "email",property = "email"),
               @Result(column = "password",property = "password"),
               @Result(column = "phoneNum",property = "phoneNum"),
               @Result(column = "status",property = "status"),
               @Result(column = "id",property = "roles",
                       javaType = List.class,many=@Many(select = "cn.szh.dao.RoleDao.findRoleByUserId"))

    })
    UserInfo findByUsername(String username);

    @Select("select * from users")
    List<UserInfo> findAll();

    @Select("select * from users where id=#{uid}")
    @Results({ @Result(id=true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "email",property = "email"),
            @Result(column = "password",property = "password"),
            @Result(column = "phoneNum",property = "phoneNum"),
            @Result(column = "status",property = "status"),
            @Result(column = "id",property = "roles",
                    javaType = List.class,many=@Many(select = "cn.szh.dao.RoleDao.findRoleByUserId"))

    })
    UserInfo findByid(int uid);


    //保存用户信息
    @Select("insert into users(email,username,password,phoneNum,status) " +
            "values(#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(UserInfo userInfo);
    //根据roleId查询用户
    @Select("select * from users where id in(select userId from users_role where roleId=#{rid})")
    List<UserInfo> findUsersByRid(int rid);

}
